<?php

class Core_Model_Customer_Product extends Dcmp_Model_Tag {

    public function __construct() {
        parent::__construct();
        $this->tagType = 'product';
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_customer_product');
    }

//    protected function beforeInsert($transaction) {
//        if (!$this->hasData('poi_id')) {
//            $this->setPoiId(App::getModel('customer/shop')->load($this->getShopId())->getPoiId());
//        }
//        parent::beforeInsert($transaction);
//    }

//    protected function insertSelf($transaction) {
//        $data = $this->getAllData();
//        if (isset($data['category_ids']) && is_array($data['category_ids'])) {
//            $data['category_ids'] = implode(',', $data['category_ids']);
//        }
//        $this->getTable()->insert($data, $transaction);
//        $this->setIdValue($this->getTable()->getInsertId($transaction));
//    }

    public function afterInsert($transaction) {
        $this->getTable()->insertRelation($this->getId(), $this->getCategoryIds(), $transaction);
        parent::afterInsert($transaction);
    }

//    protected function updateSelf($transaction) {
//        $data = $this->getChangedData();
//        if (isset($data['category_ids']) && is_array($data['category_ids'])) {
//            $data['category_ids'] = implode(',', $data['category_ids']);
//        }
//        $data['id'] = $this->getId();
//        $this->getTable()->update($data, $transaction);
//    }

    protected function afterUpdate($transaction) {
        if ($this->propertyIsChanged('category_ids')) {
            $this->getTable()->deleteRelation($this->getId(), $transaction);
            $this->getTable()->insertRelation($this->getId(), $this->getCategoryIds(), $transaction);
        }
        parent::afterUpdate($transaction);
    }

//    public function fill($data) {
//        if (isset($data['category_ids']) && !is_array($data['category_ids'])) {
//            $data['category_ids'] = explode(',', $data['category_ids']);
//        }
//        return parent::fill($data);
//    }

    public function getPoi(){
        if(!$this->hasData('poi')){
            if($this->issetData('poi_id')){
                $this->data['poi'] = App::getModel('foundation/poi')->load($this->getPoiId());
            }
        }
        return $this->data['poi'];
    }

    protected function initializeTable($table) {
        $table->setName('{t}customer_product');
        $table->createColumn('id', 'integer', TRUE, TRUE);
        $table->createColumn('name', 'string');
        $table->createColumn('introduction', 'string');
        $table->createColumn('detail', 'string');
        $table->createColumn('sef_url', 'string');
        $table->createColumn('category_ids', 'array');
        $table->createColumn('click_count', 'integer')->setDefaultValue(0);
        $table->createColumn('picture', 'string');
        $table->createColumn('price', 'numeric')->setDefaultValue(0);
        $table->createColumn('poi_id', 'string')->setUpdateable(FALSE);
//        $table->createColumn('shop_id', 'integer')->setUpdateable(FALSE);
        parent::initializeTable($table);
    }

}